package org.jivesoftware.smack.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.Random;

public class StringUtils
{
  private static final char[] AMP_ENCODE;
  private static final char[] APOS_ENCODE;
  private static final char[] GT_ENCODE;
  private static final char[] LT_ENCODE;
  private static final char[] QUOTE_ENCODE = "&quot;".toCharArray();
  private static MessageDigest digest;
  private static char[] numbersAndLetters;
  private static Random randGen;

  static
  {
    APOS_ENCODE = "&apos;".toCharArray();
    AMP_ENCODE = "&amp;".toCharArray();
    LT_ENCODE = "&lt;".toCharArray();
    GT_ENCODE = "&gt;".toCharArray();
    digest = null;
    randGen = new Random();
    numbersAndLetters = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
  }

  public static byte[] decodeBase64(String paramString)
  {
    return Base64.decode(paramString);
  }

  public static String encodeBase64(String paramString)
  {
    Object localObject = (byte[])null;
    try
    {
      byte[] arrayOfByte = paramString.getBytes("ISO-8859-1");
      localObject = arrayOfByte;
      return encodeBase64(localObject);
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      localUnsupportedEncodingException.printStackTrace();
    }
  }

  public static String encodeBase64(byte[] paramArrayOfByte)
  {
    return encodeBase64(paramArrayOfByte, false);
  }

  public static String encodeBase64(byte[] paramArrayOfByte, int paramInt1, int paramInt2, boolean paramBoolean)
  {
    int i;
    if (paramBoolean)
      i = 0;
    while (true)
    {
      return Base64.encodeBytes(paramArrayOfByte, paramInt1, paramInt2, i);
      i = 8;
    }
  }

  public static String encodeBase64(byte[] paramArrayOfByte, boolean paramBoolean)
  {
    return encodeBase64(paramArrayOfByte, 0, paramArrayOfByte.length, paramBoolean);
  }

  public static String encodeHex(byte[] paramArrayOfByte)
  {
    StringBuilder localStringBuilder = new StringBuilder(2 * paramArrayOfByte.length);
    int i = paramArrayOfByte.length;
    int j = 0;
    while (true)
    {
      if (j >= i)
        return localStringBuilder.toString();
      int k = paramArrayOfByte[j];
      if ((k & 0xFF) < 16)
        localStringBuilder.append("0");
      localStringBuilder.append(Integer.toString(k & 0xFF, 16));
      ++j;
    }
  }

  public static String escapeForXML(String paramString)
  {
    String str;
    if (paramString == null)
      str = null;
    while (true)
    {
      int i;
      int j;
      char[] arrayOfChar;
      int k;
      StringBuilder localStringBuilder;
      while (true)
      {
        return str;
        i = 0;
        j = 0;
        arrayOfChar = paramString.toCharArray();
        k = arrayOfChar.length;
        localStringBuilder = new StringBuilder((int)(1.3D * k));
        if (i < k)
          break;
        if (j != 0)
          break label327;
        str = paramString;
      }
      int l = arrayOfChar[i];
      if (l <= 62)
      {
        if (l != 60)
          break label110;
        if (i > j)
          localStringBuilder.append(arrayOfChar, j, i - j);
        j = i + 1;
        localStringBuilder.append(LT_ENCODE);
      }
      while (true)
      {
        do
          while (true)
          {
            while (true)
            {
              do
              {
                while (true)
                {
                  while (true)
                    ++i;
                  label110: if (l != 62)
                    break;
                  if (i > j)
                    localStringBuilder.append(arrayOfChar, j, i - j);
                  j = i + 1;
                  localStringBuilder.append(GT_ENCODE);
                }
                if (l != 38)
                  break label249;
                if (i > j)
                  localStringBuilder.append(arrayOfChar, j, i - j);
              }
              while ((k > i + 5) && (arrayOfChar[(i + 1)] == '#') && (Character.isDigit(arrayOfChar[(i + 2)])) && (Character.isDigit(arrayOfChar[(i + 3)])) && (Character.isDigit(arrayOfChar[(i + 4)])) && (arrayOfChar[(i + 5)] == ';'));
              j = i + 1;
              localStringBuilder.append(AMP_ENCODE);
            }
            label249: if (l != 34)
              break;
            if (i > j)
              localStringBuilder.append(arrayOfChar, j, i - j);
            j = i + 1;
            localStringBuilder.append(QUOTE_ENCODE);
          }
        while (l != 39);
        if (i > j)
          localStringBuilder.append(arrayOfChar, j, i - j);
        j = i + 1;
        localStringBuilder.append(APOS_ENCODE);
      }
      if (i > j)
        label327: localStringBuilder.append(arrayOfChar, j, i - j);
      str = localStringBuilder.toString();
    }
  }

  public static String escapeNode(String paramString)
  {
    StringBuilder localStringBuilder;
    int i;
    String str;
    if (paramString == null)
      str = null;
    while (true)
    {
      return str;
      localStringBuilder = new StringBuilder(8 + paramString.length());
      i = 0;
      int j = paramString.length();
      if (i < j)
        break;
      str = localStringBuilder.toString();
    }
    char c = paramString.charAt(i);
    switch (c)
    {
    default:
      if (!(Character.isWhitespace(c)))
        break label247;
      localStringBuilder.append("\\20");
    case '"':
    case '&':
    case '\'':
    case '/':
    case ':':
    case '<':
    case '>':
    case '@':
    case '\\':
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              while (true)
              {
                while (true)
                {
                  while (true)
                  {
                    while (true)
                    {
                      while (true)
                      {
                        while (true)
                          ++i;
                        localStringBuilder.append("\\22");
                      }
                      localStringBuilder.append("\\26");
                    }
                    localStringBuilder.append("\\27");
                  }
                  localStringBuilder.append("\\2f");
                }
                localStringBuilder.append("\\3a");
              }
              localStringBuilder.append("\\3c");
            }
            localStringBuilder.append("\\3e");
          }
          localStringBuilder.append("\\40");
        }
        localStringBuilder.append("\\5c");
      }
      label247: localStringBuilder.append(c);
    }
  }

  /**
   * @deprecated
   */
  // ERROR //
  public static String hash(String paramString)
  {
    // Byte code:
    //   0: ldc 2
    //   2: monitorenter
    //   3: getstatic 45	org/jivesoftware/smack/util/StringUtils:digest	Ljava/security/MessageDigest;
    //   6: astore_2
    //   7: aload_2
    //   8: ifnonnull +11 -> 19
    //   11: ldc 167
    //   13: invokestatic 173	java/security/MessageDigest:getInstance	(Ljava/lang/String;)Ljava/security/MessageDigest;
    //   16: putstatic 45	org/jivesoftware/smack/util/StringUtils:digest	Ljava/security/MessageDigest;
    //   19: getstatic 45	org/jivesoftware/smack/util/StringUtils:digest	Ljava/security/MessageDigest;
    //   22: aload_0
    //   23: ldc 175
    //   25: invokevirtual 75	java/lang/String:getBytes	(Ljava/lang/String;)[B
    //   28: invokevirtual 179	java/security/MessageDigest:update	([B)V
    //   31: getstatic 45	org/jivesoftware/smack/util/StringUtils:digest	Ljava/security/MessageDigest;
    //   34: invokevirtual 182	java/security/MessageDigest:digest	()[B
    //   37: invokestatic 184	org/jivesoftware/smack/util/StringUtils:encodeHex	([B)Ljava/lang/String;
    //   40: astore 4
    //   42: ldc 2
    //   44: monitorexit
    //   45: aload 4
    //   47: areturn
    //   48: astore 5
    //   50: getstatic 190	java/lang/System:err	Ljava/io/PrintStream;
    //   53: ldc 192
    //   55: invokevirtual 198	java/io/PrintStream:println	(Ljava/lang/String;)V
    //   58: goto -39 -> 19
    //   61: astore_1
    //   62: ldc 2
    //   64: monitorexit
    //   65: aload_1
    //   66: athrow
    //   67: astore_3
    //   68: getstatic 190	java/lang/System:err	Ljava/io/PrintStream;
    //   71: aload_3
    //   72: invokevirtual 201	java/io/PrintStream:println	(Ljava/lang/Object;)V
    //   75: goto -44 -> 31
    //
    // Exception table:
    //   from	to	target	type
    //   11	19	48	java/security/NoSuchAlgorithmException
    //   3	7	61	finally
    //   11	19	61	finally
    //   19	31	61	finally
    //   31	42	61	finally
    //   50	58	61	finally
    //   68	75	61	finally
    //   19	31	67	java/io/UnsupportedEncodingException
  }

  public static String parseBareAddress(String paramString)
  {
    String str;
    if (paramString == null)
      str = null;
    while (true)
    {
      int i;
      while (true)
      {
        while (true)
        {
          return str;
          i = paramString.indexOf("/");
          if (i >= 0)
            break;
          str = paramString;
        }
        if (i != 0)
          break;
        str = "";
      }
      str = paramString.substring(0, i);
    }
  }

  public static String parseName(String paramString)
  {
    String str;
    if (paramString == null)
      str = null;
    while (true)
    {
      int i;
      while (true)
      {
        return str;
        i = paramString.lastIndexOf("@");
        if (i > 0)
          break;
        str = "";
      }
      str = paramString.substring(0, i);
    }
  }

  public static String parseResource(String paramString)
  {
    String str;
    if (paramString == null)
      str = null;
    while (true)
    {
      int i;
      while (true)
      {
        return str;
        i = paramString.indexOf("/");
        if ((i + 1 <= paramString.length()) && (i >= 0))
          break;
        str = "";
      }
      str = paramString.substring(i + 1);
    }
  }

  public static String parseServer(String paramString)
  {
    String str;
    if (paramString == null)
      str = null;
    while (true)
    {
      int i;
      while (true)
      {
        while (true)
        {
          return str;
          i = paramString.lastIndexOf("@");
          if (i + 1 <= paramString.length())
            break;
          str = "";
        }
        int j = paramString.indexOf("/");
        if ((j <= 0) || (j <= i))
          break;
        str = paramString.substring(i + 1, j);
      }
      str = paramString.substring(i + 1);
    }
  }

  public static String randomString(int paramInt)
  {
    String str;
    if (paramInt < 1)
    {
      str = null;
      return str;
    }
    char[] arrayOfChar = new char[paramInt];
    int i = 0;
    while (true)
    {
      while (i >= arrayOfChar.length)
        str = new String(arrayOfChar);
      arrayOfChar[i] = numbersAndLetters[randGen.nextInt(71)];
      ++i;
    }
  }

  public static String unescapeNode(String paramString)
  {
    char[] arrayOfChar;
    StringBuilder localStringBuilder;
    int i;
    int j;
    int k;
    int l;
    String str;
    if (paramString == null)
      str = null;
    while (true)
    {
      return str;
      arrayOfChar = paramString.toCharArray();
      localStringBuilder = new StringBuilder(arrayOfChar.length);
      i = 0;
      j = arrayOfChar.length;
      if (i < j)
        break;
      str = localStringBuilder.toString();
    }
    char c = paramString.charAt(i);
    if ((c == '\\') && (i + 2 < j))
    {
      k = arrayOfChar[(i + 1)];
      l = arrayOfChar[(i + 2)];
      if (k != 50)
        break label218;
    }
    switch (l)
    {
    default:
      localStringBuilder.append(c);
    case 48:
    case 50:
    case 54:
    case 55:
    case 102:
    }
    while (true)
    {
      label302: label329: 
      do
        while (true)
        {
          do
          {
            while (true)
            {
              while (true)
              {
                while (true)
                {
                  while (true)
                  {
                    while (true)
                    {
                      while (true)
                      {
                        while (true)
                        {
                          while (true)
                          {
                            while (true)
                            {
                              while (true)
                                ++i;
                              localStringBuilder.append(' ');
                              i += 2;
                            }
                            localStringBuilder.append('"');
                            i += 2;
                          }
                          localStringBuilder.append('&');
                          i += 2;
                        }
                        localStringBuilder.append('\'');
                        i += 2;
                      }
                      localStringBuilder.append('/');
                      i += 2;
                    }
                    label218: if (k != 51)
                      break label302;
                    switch (l)
                    {
                    case 98:
                    case 100:
                    default:
                    case 97:
                    case 99:
                    case 101:
                    }
                  }
                  localStringBuilder.append(':');
                  i += 2;
                }
                localStringBuilder.append('<');
                i += 2;
              }
              localStringBuilder.append('>');
              i += 2;
            }
            if (k != 52)
              break label329;
          }
          while (l != 48);
          localStringBuilder.append("@");
          i += 2;
        }
      while ((k != 53) || (l != 99));
      localStringBuilder.append("\\");
      i += 2;
    }
  }
}